package com.yupi.picturebackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.picturebackend.common.BaseResponse;
import com.yupi.picturebackend.common.ResultUtils;
import com.yupi.picturebackend.exception.BusinessException;
import com.yupi.picturebackend.exception.ErrorCode;
import com.yupi.picturebackend.exception.ThrowUtils;
import com.yupi.picturebackend.model.dto.picture.PictureQueryRequest;
import com.yupi.picturebackend.model.dto.picturefavour.PictureFavourAddRequest;
import com.yupi.picturebackend.model.dto.picturefavour.PictureFavourQueryRequest;
import com.yupi.picturebackend.model.entity.Picture;
import com.yupi.picturebackend.model.entity.User;
import com.yupi.picturebackend.model.vo.PictureVO;
import com.yupi.picturebackend.service.PictureFavourService;
import com.yupi.picturebackend.service.PictureService;
import com.yupi.picturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 图片收藏接口
 */
@RestController
@RequestMapping("/picture_favour")
@Slf4j
public class PictureFavourController {

    @Resource
    private PictureFavourService pictureFavourService;

    @Resource
    private PictureService pictureService;

    @Resource
    private UserService userService;

    /**
     * 收藏 / 取消收藏
     *
     * @param pictureFavourAddRequest
     * @param request
     * @return resultNum 收藏变化数
     */
    @PostMapping("/")
    public BaseResponse<Integer> doPictureFavour(@RequestBody PictureFavourAddRequest pictureFavourAddRequest,
                                              HttpServletRequest request) {
        if (pictureFavourAddRequest == null || pictureFavourAddRequest.getPictureId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能操作
        final User loginUser = userService.getLoginUser(request);
        long pictureId = pictureFavourAddRequest.getPictureId();
        int result = pictureFavourService.doPictureFavour(pictureId, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 获取我收藏的图片列表
     *
     * @param pictureQueryRequest
     * @param request
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<PictureVO>> listMyFavourPictureByPage(@RequestBody PictureQueryRequest pictureQueryRequest,
                                                                HttpServletRequest request) {
        if (pictureQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long current = pictureQueryRequest.getCurrent();
        long size = pictureQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Picture> picturePage = pictureFavourService.listFavourPictureByPage(new Page<>(current, size),
                pictureService.getQueryWrapper(pictureQueryRequest), loginUser.getId());
        return ResultUtils.success(pictureService.getPictureVOPage(picturePage, request));
    }

    /**
     * 获取用户收藏的图片列表
     *
     * @param pictureFavourQueryRequest
     * @param request
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<PictureVO>> listFavourPictureByPage(@RequestBody PictureFavourQueryRequest pictureFavourQueryRequest,
            HttpServletRequest request) {
        if (pictureFavourQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = pictureFavourQueryRequest.getCurrent();
        long size = pictureFavourQueryRequest.getPageSize();
        Long userId = pictureFavourQueryRequest.getUserId();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20 || userId == null, ErrorCode.PARAMS_ERROR);
        Page<Picture> postPage = pictureFavourService.listFavourPictureByPage(new Page<>(current, size),
                pictureService.getQueryWrapper(pictureFavourQueryRequest.getPictureQueryRequest()), userId);
        return ResultUtils.success(pictureService.getPictureVOPage(postPage, request));
    }
}
